package com.zzyl.controller;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.UserDto;
import com.zzyl.exception.EmailChangedException;
import com.zzyl.service.SysUserService;
import com.zzyl.vo.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Set;

@RestController
@RequestMapping("/user")
@Api(tags = "用户管理")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("分页查询用户列表")
    public ResponseResult<PageResponse<UserVo>> getUserByPage(
            @ApiParam(value = "用户信息")
            @RequestBody
            UserDto userDto,
            @ApiParam(value = "排序")
            @PathVariable("pageNum") Integer pageNum,
            @ApiParam(value = "每页显示数量")
            @PathVariable("pageSize") Integer pageSize) {
        PageResponse<UserVo> page = sysUserService.getUserByPage(userDto, pageNum, pageSize);
        return ResponseResult.success(page);
    }

    @PostMapping("/list")
    @ApiOperation("用户列表")
    public ResponseResult<Set<UserVo>> getUserList(
            @ApiParam(value = "真实姓名")
            String realName
    ) {
        return ResponseResult.success(sysUserService.getUserList(realName));
    }

    @PutMapping
    @ApiOperation("新增用户")
    public ResponseResult createUser(@RequestBody UserDto userDto) {
        sysUserService.createUser(userDto);
        return ResponseResult.success();
    }

    @PatchMapping
    @ApiOperation("修改用户")
    public ResponseResult updateUser(@RequestBody UserDto userDto) {
        sysUserService.updateUser(userDto);
        return ResponseResult.success();
    }

    @PutMapping("/is-enable/{id}/{status}")
    @ApiOperation("修改用户状态")
    public ResponseResult updateUserState(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        sysUserService.updateUserState(id, status);
        return ResponseResult.success();
    }

    @DeleteMapping("remove/{userIds}")
    @ApiOperation("删除用户")
    public ResponseResult deleteUser(@PathVariable("userIds") Long[] userIds) {
        sysUserService.deleteUser(userIds);
        return ResponseResult.success();
    }

    @PostMapping("/reset-passwords/{userId}")
    @ApiOperation("重置密码")
    public ResponseResult resetPasswords(@PathVariable("userId") Long userId) {
        sysUserService.resetPasswords(userId);
        return ResponseResult.success();
    }

    @ExceptionHandler(EmailChangedException.class)
            public RedirectView handleEmailChangedException(EmailChangedException ex){
            return new RedirectView("/Login");

            }

//    @GetMapping("/current-user")
//    @ApiOperation("获取当前用户信息")
//    public ResponseResult currentUser() {
//        return ResponseResult.success(sysUserService.getCurrentUser());
//    }
}
